package net.sky.oa.service.shortmsg.impl;

import javax.annotation.Resource;

import net.sky.oa.dao.shortmsg.SmsEmployeeDao;
import net.sky.oa.model.SmsEmployee;
import net.sky.oa.model.SmsInfo;
import net.sky.oa.service.shortmsg.SmsEmployeeService;
import net.sky.oa.service.shortmsg.SmsInfoService;
import net.sky.oa.util.SmsUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htsoft.core.service.impl.BaseServiceImpl;

@Service
@Transactional
public class SmsEmployeeServiceImpl extends BaseServiceImpl<SmsEmployee>
		implements SmsEmployeeService {
	
	@Autowired
	private SmsInfoService smsInfoService;
	
	private SmsEmployeeDao smsEmployeeDao;

	public SmsEmployeeDao getSmsEmployeeDao() {
		return smsEmployeeDao;
	}
	
	@Resource
	public void setSmsEmployeeDao(SmsEmployeeDao smsEmployeeDao) {
		super.setDao(smsEmployeeDao);
		this.smsEmployeeDao = smsEmployeeDao;
	}

	public int saveSmsEmployee(SmsInfo smsInfo,SmsEmployee smsEmployee,String mobiles) throws Exception {
		int i = SmsUtil.sendMsg(smsInfo,smsEmployee.getContent(), mobiles);
		if(i!=1) {
		} else {
			smsEmployeeDao.save(smsEmployee);
			this.setSmsInfo(smsInfo, smsEmployee);
			smsInfoService.save(smsInfo);
		}
		return i;
	}
	
	public void setSmsInfo(SmsInfo smsInfo,SmsEmployee smsEmployee) {
		if(null != smsInfo){
			int sendCount = smsInfo.getSendcount();
			int remaincount = smsInfo.getRemaincount();
			smsInfo.setSendcount(sendCount+smsEmployee.getCount());
			smsInfo.setRemaincount(remaincount-smsEmployee.getCount());
		}
	}
	

}
